package com.dstz.bpm.act.util;

import cn.hutool.core.util.ObjectUtil;
import com.dstz.base.core.util.AppUtil;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;

public class ActivitiUtil {
   public static CommandExecutor getCommandExecutor() {
      ProcessEngineImpl engine = (ProcessEngineImpl)AppUtil.getBean(ProcessEngine.class);
      CommandExecutor cmdExecutor = engine.getProcessEngineConfiguration().getCommandExecutor();
      return cmdExecutor;
   }

   public static Map<String, Object> a(String actDefId, String nodeId, String[] aryDestination) {
      Map<String, Object> map = new HashMap();
      RepositoryService repositoryService = (RepositoryService)AppUtil.getBean(RepositoryService.class);
      ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity)((RepositoryServiceImpl)repositoryService).getDeployedProcessDefinition(actDefId);
      ActivityImpl curAct = processDefinition.findActivity(nodeId);
      List outTrans = curAct.getOutgoingTransitions();

      try {
         List<PvmTransition> cloneOutTrans = (List)ObjectUtil.cloneByStream(outTrans);
         map.put("outTrans", cloneOutTrans);
      } catch (Exception var14) {
         var14.printStackTrace();
      }

      Iterator it = outTrans.iterator();

      while(it.hasNext()) {
         PvmTransition transition = (PvmTransition)it.next();
         PvmActivity activity = transition.getDestination();
         List<PvmTransition> inTrans = activity.getIncomingTransitions();
         Iterator itIn = inTrans.iterator();

         while(itIn.hasNext()) {
            PvmTransition inTransition = (PvmTransition)itIn.next();
            if (inTransition.getSource().getId().equals(curAct.getId())) {
               itIn.remove();
            }
         }
      }

      curAct.getOutgoingTransitions().clear();
      if (aryDestination != null && aryDestination.length > 0) {
         String[] var16 = aryDestination;
         int var17 = aryDestination.length;

         for(int var18 = 0; var18 < var17; ++var18) {
            String dest = var16[var18];
            ActivityImpl destAct = processDefinition.findActivity(dest);
            TransitionImpl transitionImpl = curAct.createOutgoingTransition();
            transitionImpl.setDestination(destAct);
         }
      }

      map.put("activity", curAct);
      return map;
   }

   public static void a(Map<String, Object> map) {
      ActivityImpl curAct = (ActivityImpl)map.get("activity");
      List<PvmTransition> outTrans = (List)map.get("outTrans");
      curAct.getOutgoingTransitions().clear();
      curAct.getOutgoingTransitions().addAll(outTrans);
   }
}
